package cn.jiyun.controller;


import cn.jiyun.common.config.Result;
import cn.jiyun.entity.XmUser;
import cn.jiyun.service.IXmUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@RestController
//@CrossOrigin
@RequestMapping("/xm-client/xm-user")
public class XmUserController {


    @Autowired
    private IXmUserService xmUserService;


    @RequestMapping("findAll")
    public Result findAll(Integer current, Integer pageSize, @RequestBody XmUser xmUser) {
        Page<XmUser> xmUsers = xmUserService.findAll(current, pageSize, xmUser);
        return Result.ok(xmUsers);
    }

    @RequestMapping("lockk")
    public Result lockk(@RequestBody XmUser xmUser) {
        xmUserService.updateById(xmUser);
        return Result.ok();
    }


    //    根据id查询一个用户的信息
    @RequestMapping("findById")
    public Result findById(Integer id) {
        XmUser xmUser = xmUserService.getById(id);
        return Result.ok(xmUser);
    }


    //    统计本周新增用户
    @RequestMapping("findAllUser")
    public Result findAllUser() {
        Integer count = xmUserService.findAllUser();
        return Result.ok(count);
    }


    //    统计昨日的新增用户
    @RequestMapping("findAllUser2")
    public Result findAllUser2() {
        Integer count = xmUserService.findAllUser2();
        return Result.ok(count);
    }

//  统计所有的审核信息
    @RequestMapping("countStatus")
    public Result countStatus() {
        Integer[] count=xmUserService.countStatus();
        return Result.ok(count);
    }



//    密码登录
    @RequestMapping("mmLogin")
    public Result mmLogin(@RequestBody XmUser xmUser) {
        XmUser xmUser1=xmUserService.mmLogin(xmUser);
        return Result.ok(xmUser1);
    }



    //    判断redia中是否又用户信息
    @RequestMapping("pdUser")
    public Result pdUser(@RequestBody XmUser xmUser){
        XmUser xmUser1=xmUserService.paUser(xmUser);
        return Result.ok(xmUser1);
    }


//    注册用户的信息
    @RequestMapping("zcUser")
    public Result zcUser(@RequestBody XmUser xmUser){
        xmUser.setLastAccessedTime(LocalDateTime.now());
        xmUser.setRegistration(LocalDateTime.now());
        QueryWrapper<XmUser> xmUserQueryWrapper = new QueryWrapper<>();
        xmUserQueryWrapper.eq("mobile_phone", xmUser.getMobilePhone());
        XmUser user = xmUserService.getOne(xmUserQueryWrapper);
        if(user!=null){
            return Result.ok(user);
        }
        xmUser.setPassword("11111111");
        SecureRandom secureRandom = new SecureRandom();
        long randomNumber = 1000000000L + (long) (secureRandom.nextDouble() * 9000000000L);
        xmUser.setName("用户"+randomNumber);
        xmUserService.save(xmUser);
        return Result.ok(xmUser);
    }



//    修改用户的密码
    @RequestMapping("updateUser")
    public Result updateUser(@RequestBody XmUser xmUser){
        QueryWrapper<XmUser> xmUserQueryWrapper = new QueryWrapper<>();
        xmUserQueryWrapper.eq("mobile_phone", xmUser.getMobilePhone());
        XmUser xmUser1 = xmUserService.getOne(xmUserQueryWrapper);
        xmUser1.setPassword(xmUser.getPassword());
        xmUserService.updateById(xmUser1);
        return Result.ok();
    }
}
